package org.example.opus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.example.OpusFeignClient;
import org.example.ai.model.entity.Users;
import org.example.ai.model.entity.Works;
import org.example.opus.mapper.WorksMapper;
import org.example.opus.service.IWorksService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 作品表，存储用户发布的作品信息及相关属性 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-17
 */

@Slf4j
@Service
public class WorksServiceImpl extends ServiceImpl<WorksMapper, Works> implements IWorksService {

    @Autowired
    private WorksMapper worksMapper;
    @Autowired
    private OpusFeignClient opusFeignClient;


//    @Override
//    public List<Works> worksList(Works works) {
//        LambdaQueryWrapper<Works> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(Works::getWorkId,works.getWorkId());
//        lambdaQueryWrapper.eq(Works::getUserId,works.getUserId());
//        List<Works> wor = this.baseMapper.selectList(lambdaQueryWrapper);
//        return wor;
//    }

    @Override
    public List<Works> getWork(Integer userId) {
        QueryWrapper<Works> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        List<Works> works = worksMapper.selectList(queryWrapper);
        System.out.println(works);
        log.info(userId.toString());
        return works;
    }

    @Override
    public Works getAllWorks(Integer workId) {
        List<Users> usersList = opusFeignClient.usersList();
        HashMap<Integer, String> map = new HashMap<>();
        for (Users user : usersList){
            map.put(user.getUserId(),user.getNickname());
            map.put(user.getUserId(),user.getAvatar());
        }
        Works works = worksMapper.getAllWorks(workId);
        works.setNickname(map.get(works.getUserId()));
        works.setAvatar(map.get(works.getUserId()));
        return works;
    }

    @Override
    public Works getWorkAdd(Works works) {
        worksMapper.insert(works);
        return works;
    }

    @Override
    public Works getWorkList(Integer workId) {
        Works works = worksMapper.selectById(workId);
        return works;
    }


}
